import numpy as np
import tensorflow as tf
from tensorflow.python.client.session import Session
# from keras.layers import Concatenate, Input, Lambda, UpSampling2D
# from keras.models import Model
# from tensorflow._api.v1 import dtypes
from utils.utils import compose

y_true = tf.constant(value=[[[[0.1,0.1],[0.2,0.2],[0.3,0.3],[0.4,0.4]],\
    [[0.1,0.1],[0.2,0.2],[0.3,0.3],[0.4,0.4]],\
    [[0.1,0.1],[0.2,0.2],[0.3,0.3],[0.4,0.4]],\
    [[0.1,0.1],[0.2,0.2],[0.3,0.3],[0.4,0.4]]]],dtype=tf.float32)
print(y_true.shape)

y_pre = tf.constant(value=[[[[0.1,0.1],[0.2,0.2],[0.3,0.3],[0.4,0.4]],\
    [[0.7,0.7],[0.8,0.8],[0.9,0.9],[0.0,0.0]],\
    [[0.1,0.1],[0.2,0.2],[0.3,0.3],[0.4,0.4]],\
    [[0.0,0.0],[0.1,0.1],[0.2,0.2],[0.3,0.3]]]],dtype=tf.float32)
print(y_pre.shape)

v = y_true-y_pre
print(v.shape)
with tf.compat.v1.Session() as sess:
    print(v.eval())

g = abs(v)
with tf.compat.v1.Session() as sess:
    print(g.eval())

bins = 30
edges = np.array([i/bins for i in range(bins + 1)])
# print(edges.shape)
# print(edges)


edges = np.expand_dims(np.expand_dims(edges, axis=-1), axis=-1)
# print(edges.shape)
# print(edges)

gr = tf.constant(value = [[[0.1],[0.2],[0.3]],[[0.4],[0.5],[0.6]],[[0.7],[0.8],[0.9]]])
print("gr shape: ",gr.shape)

# 分成三个区间 0-0.3->7  0.3-0.7->4  0.7-1->5
p1 = tf.logical_and(tf.greater_equal(gr,0),tf.less(gr,0.3))
p1 = tf.cast(p1,tf.float32)
p1_sum = tf.reduce_sum(p1)
p1 = p1*7
with tf.compat.v1.Session() as sess:
    print(p1.eval())
    print("p1_sum:",p1_sum.eval())


p2 = tf.logical_and(tf.greater_equal(gr,0.3),tf.less(gr,0.7))
p2 = tf.cast(p2,tf.float32)
p2_sum = tf.reduce_sum(p2)
p2 = p2*4
with tf.compat.v1.Session() as sess:
    print(p2.eval())
    print("p2_sum:",p2_sum.eval())

p3 = tf.logical_and(tf.greater_equal(gr,0.7),tf.less_equal(gr,1))
p3 = tf.cast(p3,tf.float32)
p3 = p3*5

p = p1+p2+p3
with tf.compat.v1.Session() as sess:
    print(p.eval())

# positives = tf.keras.backend.greater_equal(gr, 0.5)
# print(positives)
# with tf.compat.v1.Session() as sess:
#     print(positives.eval())

# positives = tf.keras.backend.cast(positives, tf.keras.backend.floatx())
# print(positives)
# with tf.compat.v1.Session() as sess:
#     print(positives.eval())
#     print((1-positives).eval())
#     print(((1-positives)*gr).eval())



# positives = k.backend.cast(positives, k.backend.floatx())

# encoder_outputs = positives + ((1-positives)*encoder_outputs)
# with tf.compat.v1.Session() as sess:
#     n = gr.eval()
# print(n)
# n[(n>=0.3)&(n<0.7)] = 10
# print(n)

# print("edges[:-1, :, :]",edges[:-1, :, :])
# print("edges[1:, :, :]",edges[1:, :, :])
# r = tf.greater_equal(gr, edges[:-1, :, :])
# r = tf.greater_equal(gr, 0.3)
# print("r.shape: ",r.shape)
# with tf.compat.v1.Session() as sess:
#     print(r.eval())
# 大于等于0.3小于0.7
# greater = a>=0.3
# less = a<0.7
# grater_less = tf.logical_and(greater,less)
# r = tf.where(grater_less,tf.ones_like(a)*10,a)
# with tf.compat.v1.Session() as sess:
#     print(r.eval())

# acc_sum = tf.zeros(shape=(bins,), dtype=tf.float32)
# print(acc_sum.shape)
# print(acc_sum)
# with tf.compat.v1.Session() as sess:
#     print(acc_sum.eval())

# epsilon = tf.keras.backend.epsilon()
# print("epsilon = ",epsilon)

# y_pre = tf.keras.backend.clip(y_pre,0.4,0.9)
# # print(y_pre)
# with tf.compat.v1.Session() as sess:
#     print(y_pre.eval())
